package com.sywb.chuangyebao;

import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.sywb.chuangyebao.view.MainAtlasActivity;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ApkUtils;
import org.bining.footstone.utils.SharedUtils;

import java.util.Map;

/**
 * 阿里推送消息处理
 */
public class AliMessageReceiver extends MessageReceiver {

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        // 普通消息 目前仅私信为普通消息
        Logger.e("AliMessageReceiver onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent() + ",traceInfo" + cPushMessage.getTraceInfo());

        RxBus.get().post(Constants.USERCHAT, Constants.USERCHAT); // 更新个人中心页面提示的消息数量
        RxBus.get().post(Constants.POST_CHAT_MESSAGE, cPushMessage.getContent()); // 更新私信会话和内容
    }

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // 通知消息
        Logger.e("AliMessageReceiver onNotification, title: " + title + ", summary: " + summary + ", extraMap: " + JSON.toJSONString(extraMap));
        if (extraMap.containsKey("topic_type")) {
            String type = extraMap.get("topic_type");
            if (type.equals("notifi") || type.equals("notice")) {
                RxBus.get().post(Constants.USERCHAT, Constants.USERCHAT); // 更新个人中心页面提示的消息数量
            }
        }

        // 旧的type 1 创TV预约（通知） 2 通知 3 私信 4 公告

        // 主题Type》》article:文章详情页 video:视频详情页 wenda 问答详情页 topic 话题聚合页 video_train 培训视频详情页 article_train 培训文章详情页
        // url 指定链接地址 notice 公告 chuangtv 创TV project 项目  notifi 通知  Reservation 创TV预约
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        // 通知消息被打开
        Logger.e("AliMessageReceiver onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + JSON.toJSONString(extraMap));
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        // 无跳转逻辑通知打开
        Logger.e("AliMessageReceiver onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + JSON.toJSONString(extraMap));

        Map<String, String> map = JSON.parseObject(extraMap, new TypeReference<Map<String, String>>() {
        });

        if (map.containsKey("topic_type")) {

            SharedUtils.put(Constants.USERMESSAGERECEIVER, extraMap);

            if (!ApkUtils.isAppInBackground(context)) {
                RxBus.get().post(Constants.USERMESSAGERECEIVER, Constants.USERMESSAGERECEIVER);
            } else {
                Intent intent = new Intent(context, MainAtlasActivity.class);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setAction(Intent.ACTION_MAIN);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//                Intent intent = new Intent(context, MainAtlasActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        }
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        // 自定义通知在应用内到达
        Logger.e("AliMessageReceiver onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + JSON.toJSONString(extraMap) + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        // 通知删除
        Logger.e("AliMessageReceiver onNotificationRemoved");
    }

}
